function fixedLength(textareaID,wordLength)
{	
    var textarea = document.getElementById(textareaID);

    textarea.onkeyup = function() {onkeyupLength(textarea,wordLength);};
    textarea.onpaste = function ()
    {
        if (textarea.value.length >= wordLength)
        {
            var noneStr = "";
            window.clipboardData.setData("text",noneStr);
        }
        else
        {
            window.clipboardData.setData("text",window.clipboardData.getData("text").substr(0,wordLength - textarea.value.length));
            var diffLength = textarea.value.length + window.clipboardData.getData("text").length;
        }
    };
    
    textarea.ondrop = function ()
    {
     	 if (textarea.value.length >= wordLength){
         var noneStr = "";
            window.event.dataTransfer.setData("text",noneStr);
     }
        else
        {
            window.event.dataTransfer.setData("text",window.event.dataTransfer.getData("text").substr(0,wordLength - textarea.value.length));
            var diffLength = textarea.value.length + window.event.dataTransfer.getData("text").length;
        }    
   }
}

function cutTextContent(id)
{
	if($j("#"+id).val().length>$j("#"+id).attr("maxlength"))
	{
		$j("#"+id).val($j("#"+id).val().substring(0,$j("#"+id).attr("maxlength")));
	}
}

function onkeyupLength(textarea,wordLength)
{
    var textareaLength = textarea.value.length;
    
    if (wordLength - textareaLength >= 0){
   	  
   	}
    else{
  		textarea.value = textarea.value.substr(0,wordLength);
    }
} 

function htmlEscape(str){
    if (typeof(str) == 'string') {
        return str.replace(/&/gi, "&amp;").replace(/\"/gi, "&quot;").replace(/</gi, "&lt;").replace(/>/gi, "&gt;");
    }
    return str;
}

function checkInt(e){
	var num = $j(e).val();
	if(num&&!isNaN(num)&&parseFloat(num)>=0){       
		if(num.indexOf('e')>=0){
			$j(e).val("");
			return;
		}
		$j(e).val(parseInt(parseFloat(num).toFixed(0)));
		
		$j(".float-msg").hide();
		if($j(e).attr("less")&& $j("#"+$j(e).attr("less"))){
			var lessValue=$j("#"+$j(e).attr("less")).val();
			if(lessValue&&parseFloat(lessValue)<parseFloat($j(e).val())){
				showFloatError($j(e),$j(e).attr("labelName")+" should less than "+$j("#"+$j(e).attr("less")).attr("labelName"));
				setTimeout(function(){$j(e).val("")},50);
			}
		}
		if($j(e).attr("greater")&& $j("#"+$j(e).attr("greater"))){
			var greaterValue=$j("#"+$j(e).attr("greater")).val();
			if(greaterValue&&parseFloat(greaterValue)>parseFloat($j(e).val())){
				showFloatError($j(e),$j(e).attr("labelName")+" should greater than "+$j("#"+$j(e).attr("greater")).attr("labelName"));
				setTimeout(function(){$j(e).val("")},50);
			}
		}
	}else{
		$j(e).val("");
	}
	
}

function checkFloat(e){
	var num = $j(e).val();
	if(num&&!isNaN(num)&&parseFloat(num)>=0){      
		if(num.indexOf('e')>=0){
			$j(e).val("");
			return;
		}
		if($j(e).attr("decimal")){
			num = parseFloat(num).toFixed(parseFloat($j(e).attr("decimal")));
			if(num % 1 == 0){
				$j(e).val(parseInt(num));
			}else{
				$j(e).val(num);
			}
		}
		
		$j(".float-msg").hide();
		if($j(e).attr("less")&& $j("#"+$j(e).attr("less"))){
			var lessValue=$j("#"+$j(e).attr("less")).val();
			if(lessValue&&parseFloat(lessValue)<parseFloat($j(e).val())){
				showFloatError($j(e),$j(e).attr("labelName")+" should less than "+$j("#"+$j(e).attr("less")).attr("labelName"));
				setTimeout(function(){$j(e).val("")},50);
			}
		}
		if($j(e).attr("greater")&& $j("#"+$j(e).attr("greater"))){
			var greaterValue=$j("#"+$j(e).attr("greater")).val();
			if(greaterValue&&parseFloat(greaterValue)>parseFloat($j(e).val())){
				showFloatError($j(e),$j(e).attr("labelName")+" should greater than "+$j("#"+$j(e).attr("greater")).attr("labelName"));
				setTimeout(function(){$j(e).val("")},50);
			}
		}
		
	}else{
		$j(e).val("");
	}
}
function showFloatError(src, err) {
	if(!src.siblings("div.float-msg").html()){
		var html = "<div class='float-msg'><div class='float-msg-content'><p></p></div></div>";
		src.after($j(html));
	}
	src.siblings("div.float-msg").find("p").text(err);
	src.siblings("div.float-msg").show("fast");
};

function showErrorMsg(errors){
	showErrorMsgWithId("errorMsg",errors);
}

function showErrorMsgWithId(errorId,errors){
	var innerHtml = "<div class='context-msg'><div class='msg-content alert'>"
	if(typeof(errors)=='object' && errors.constructor==Array){
		if(errors!=null && errors.length!=0){
			for(var i=0; i<errors.length; i++){
				if(errors[i].indexOf("<p>")!=0){
					errors[i]="<p>"+errors[i]+"</p>";
				}
				innerHtml+=errors[i];
			}
		}
	}
	else{
		innerHtml+="<p>"+errors+"</p>";
	}
	innerHtml+="</div></div>";  
	$j("#"+errorId).empty();
	$j("#"+errorId).append(innerHtml);
}